#pragma once

#include <Windows.h>
#include "agg_rendering_buffer.h"
#include "agg_rasterizer_scanline_aa.h"
#include "agg_color_rgba.h"
#include "agg_path_storage.h"
#include "agg_conv_smooth_poly1.h"

typedef agg::rgba8 Color;

enum STROKE_STATE {
	STROKE_NONE,
	STROKE_START,
	STROKE_MIDDLE,
	STROKE_END
};

class Document
{
private:
	agg::rendering_buffer aggbuf, aggbuftemp;
	agg::path_storage patha, pathb, patht;
	agg::rasterizer_scanline_aa<> tras;

	HBITMAP hBmp, hBmptemp;
	HDC bufDC, bufDCtemp;
	Color penColor;

	int _width, _height;

	STROKE_STATE stroke;

public:
	Document() : stroke( STROKE_NONE ), penColor( agg::rgba(0,0,0) ) {};
	~Document(void);

	int Create( int width, int height );

	void Render( HDC hdc, int x, int y ) {
		if( stroke == STROKE_MIDDLE ) {
			BitBlt( hdc, x, y, _width, _height, bufDC, 0, 0, SRCCOPY);
			BitBlt( hdc, x, y, _width, _height, bufDCtemp, 0, 0, SRCCOPY);
		}
		else
			BitBlt( hdc, x, y, _width, _height, bufDC, 0, 0, SRCCOPY);
	}
	void DrawLine( double x1, double y1, double x2, double y2, double width1, double width2 );
	void BeginStroke( double x, double y );
	void EndStroke();
	void DiscardLine() { stroke = STROKE_MIDDLE; }

	COLORREF getPenColorRef() { return RGB( penColor.r, penColor.g, penColor.b); }
	void setPenColor( COLORREF c ) { penColor = agg::rgba8( GetRValue(c), GetGValue(c), GetBValue(c), 115 ); }

	int getWidth() { return _width; }
	int getHeight() { return _height; }
};

